/*
 * @Description:
 * @Author: Haor
 * @Date: 2022-07-30 21:23:45
 * @LastEditTime: 2022-08-01 21:47:48
 * @LastEditors: Haor
 */
import { Controller, Get, Post, Put, Body, Query, Param, Delete, Res } from "@nestjs/common";
import { JobService } from "./job.service";
import { CreateJobDto, QueryJobDto } from "@dto/job-dto";
import { Permission } from "@decorators";
import { ListQueryDto } from "@dto/list-query.dto";
import { BatchDeleteDto } from "@dto/public.dto";
import { Response } from 'express'
import * as path from "path";
import { ServerException } from "@exceptions";
import { HttpResult } from "@libs/http.result";

@Controller('job')
export class JobController {
    constructor(private readonly  jobService: JobService) {}

	@Post()
    @Permission('job::query')
	finAll(@Body() queryJobDto: QueryJobDto) {
	    return this.jobService.queryJobList(queryJobDto)
    }

    // 创建职位
    @Post()
    @Permission('job::create')
    createJob(@Body() createJobDto: CreateJobDto) {
        return this.jobService.createJob(createJobDto)
    }

    // 启用、禁用
    @Put('enable/:id')
    @Permission('job::enable')
    enableJob(@Param('id') jobId:string) {
        return this.jobService.enableJob(+jobId)
    }

    // 删除职位
    @Delete('delete/:id')
    @Permission('job::delete')
    deleteJob(@Param('id') jobId:string ) {
        return this.jobService.deleteJob(jobId)
    }

    // 批量删除职位
    @Delete('batch')
    @Permission('job::batchDelete')
    batchDeleteJob(@Body() params: BatchDeleteDto ) {
        return this.jobService.batchDeleteJob(params.list)
    }

    // 导出职位
    @Get('export')
    @Permission('job::export')
    async exportJob(@Res() res: Response) {
        const filePath = path.join(process.cwd(), 'package.json')
        res.download(filePath, (err) => {
            if (err) {
                throw new ServerException(err.message)
            }
            res.send(HttpResult.success())
        })

    }
}
